package com.example.blackbox.Data;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class JsonTool {

    /**
     * 获取新闻数据
     * @param data json数据
     * @return News的ArrayList
     */
    public static  ArrayList<News> getNews(String data){
        ArrayList<News> result=new ArrayList<>();
        try{
            JSONArray dataArray=new JSONArray(data);
            for (int i = 0; i < dataArray.length(); i++) {
                if (i==0){
                    String string = dataArray.getString(i);
                    JSONObject object=new JSONObject(string);
                    News news=new News();
                    ArrayList<String> banner=new ArrayList<>();
                    Log.d("testjson",object.getString("图片1"));
                    banner.add(object.getString("图片1"));
                    banner.add(object.getString("图片2"));
                    banner.add(object.getString("图片3"));
                    news.setBanner(banner);
                    result.add(news);
                }else {
                    String string = dataArray.getString(i);
                    JSONObject object=new JSONObject(string);
                    News news=new News();
                    news.setNewsTitle(object.getString("标题"));
                    news.setNewsTag(object.getString("类别"));
                    news.setNewsDate(object.getString("时间"));
                    news.setImgUrl(object.getString("标题图片"));
                    news.setNewsMessage(object.getString("消息"));
                    news.setLevel(object.getString("作者等级"));
                    news.setAuthor(object.getString("作者"));
                    news.setAuthorHead(object.getString("作者头像"));
                    news.setInfo(object.getString("内容"));
                    result.add(news);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 获取游戏库数据
     * @param data
     * @return
     */
    public static ArrayList<GameL> getGameL(String data){
        ArrayList<GameL> gameLS=new ArrayList<>();
        try{
            JSONArray dataArray=new JSONArray(data);
            for (int i = 0; i < dataArray.length(); i++) {
               JSONObject object=new JSONObject(dataArray.getString(i));
               GameL gameL=new GameL();
               gameL.setGameImg(object.getString("gameImg"));
               gameL.setGameName(object.getString("gameName"));
               gameL.setGameTag(object.getString("gameTag"));
               gameL.setGamePrice(object.getString("gamePrice"));
                gameLS.add(gameL);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  gameLS;
    }


    /**
     * 获取轮播数据
     * @param data
     * @return
     */
    public static Banner getBanner(String data){
        Banner banner=new Banner();
        try{
            JSONObject object=new JSONObject(data);
            banner.setImg1(object.getString("图片1"));
            banner.setImg2(object.getString("图片2"));
            banner.setImg3(object.getString("图片3"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return  banner;
    }


    /**
     * 获取我的游戏数据
     * @param data
     * @return
     */
    public static ArrayList<Game> getGame(String data){
        ArrayList<Game> games=new ArrayList<>();
        try{
           JSONArray array=new JSONArray(data);
            for (int i = 0; i < array.length(); i++) {
                JSONObject object=new JSONObject(array.getString(i));
                Game game=new Game();
                game.setGameImgUrl(object.getString("gameImg"));
                game.setGameName(object.getString("gameName"));
                game.setGameTime(object.getString("gameTime"));
                games.add(game);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return games;
    }
}
